//
//  PersonInfoVC.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/13.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  个人信息

import UIKit

class PersonInfoVC: MeBaseViewController {
    
    
    ///性别
    @IBOutlet weak var sex: UITextField!
    
    ///ID号码
    @IBOutlet weak var idNum: UITextField!
    
    ///省份
    @IBOutlet weak var province: UITextField!
    
    ///城市
    @IBOutlet weak var city: UITextField!
    
    ///县区
    @IBOutlet weak var distinct: UITextField!
    
    ///街道
    @IBOutlet weak var street: UITextField!
    
    ///生日
    @IBOutlet weak var birthday: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.navigationItem.title = "个人信息"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension PersonInfoVC {
    
    /**
     ## 提前加载数据
     */
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        guard let token = localSave.object(forKey: userToken) as? String else {
            MBManager.showBriefAlert("请先登陆")
            
            return
        }
        
        //参数
        let param = ["token" : token]
        
        //发送请求
        postWithPath(path: baseinfoURL, paras: param, success: { (response) in
            
            guard let dic = response as? NSDictionary else {
                return
            }
            
            print("\((#file as NSString).lastPathComponent):(\(#line))")
            print(dic)
            print("\((#file as NSString).lastPathComponent):(\(#line))")
            
            //提取提示语
            guard let alertmsg = dic["resultmsg"] as? String else {
                return
            }
            
            if alertmsg == "成功" {
                
            }
            
            
        }) { (error) in
            print(error.localizedDescription)
        }
    }
    
    
}

// MARK:- 更新/修改用户资料
extension PersonInfoVC {
    
    ///保存信息
    @IBAction func saveInfo(_ sender: UIButton) {
        guard let token = localSave.object(forKey: userToken) as? String else {
            MBManager.showBriefAlert("请先登陆")
            
            return
        }
        
        
        let param = ["token" : token,
                     "sex" : self.sex.text!,
                     "birthday" : self.birthday.text!,
                     "idcard" : self.idNum.text!,
                     "province" : self.province.text!,
                     "city" : self.city.text!,
                     "county" : self.distinct.text!,
                     "street" : self.street.text!]
        
        //判断所填信息是否符合发送网络请求
        if self.sex.text?.characters.count == 0 {
            MBManager.showBriefAlert("性别不能为空")
            
        } else if self.birthday.text?.characters.count == 0 {
            MBManager.showBriefAlert("生日不能为空")
            
        } else if self.idNum.text?.characters.count == 0 {
            MBManager.showBriefAlert("身份证号码不能为空")
            
        } else if self.province.text?.characters.count == 0 {
            MBManager.showBriefAlert("所在省份不能为空")
            
        } else if self.city.text?.characters.count == 0 {
            MBManager.showBriefAlert("所在城市不能为空")
            
        } else if self.distinct.text?.characters.count == 0 {
            MBManager.showBriefAlert("所在县区不能为空")
            
        } else if self.street.text?.characters.count == 0 {
            MBManager.showBriefAlert("所在城市街道不能为空")
            
        } else {
            postWithPath(path: updpersoninfoURL, paras: param, success: { (response) in
                
                guard let dic = response as? NSDictionary else {
                    return
                }
                
                print(dic)
                
                //提取提示语
                guard let alertmsg = dic["resultmsg"] as? String else {
                    return
                }
                
                if alertmsg == "成功" {
                    MBManager.showBriefAlert("设置用户信息成功")
                }
                
                
            }, failure: { (error) in
                print(error.localizedDescription)
            })
        }
    
    }
    
}
